package com.heima.common.exception;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Configuration
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理不可预知异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResponseResult handlerException(Exception e){
        log.error("捕获到 Exception 异常，异常信息：{}", e);
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "网络堵车了，请稍后重试");
    }

    @ExceptionHandler(CustomizeException.class)
    public ResponseResult handlerCustomizeException(CustomizeException e){
        log.error("拦截到 CustomizeException 异常，异常信息：{}", e);
        return ResponseResult.errorResult(e.getAppHttpCodeEnum());
    }

}
